<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>管理已上架的商品</title>
		<link rel="stylesheet" type="text/css" href="css/goodsManage.css"/>
		<script src="js/jquery-2.1.0.js"></script>

	</head>
	<body>
			<div id="back">
			<a href="#" onClick="javascript :history.back(-1);"><&nbsp;返回</a><!--将返回的地址填入-->
		</div>


			<!-- hbl---添加一个div来放从数据库拿来的东西-->
			<div id="mySql">

			</div>


		<!--对已上架的商品进行管理，有编辑与下架，编辑只能对数量与价格进行修改-->
		<!--本页是卖家商品列表页-->
		<!--<div class="sellerGoodsList">-->
			<!--<img src="img/panda.jpg"/>-->
			<!-- -->
			<!--<ul  class="goodsInfo">&lt;!&ndash;这里只列出这写，商品的描述在商品详情页展示&ndash;&gt;-->
				<!--<li class="goodsName">名称:aaaaaaa</li>-->
				<!--<li class="goodsPlace">产地:aaaaaaa</li>-->
				<!--<li class="goodsPrice">价格:<span class="priceNum">10/袋</span></li>-->
				<!--<li class="goodsNum">数量:<span class="number">100</span></li>-->
			<!--</ul>-->

			<!--<div class="goodsButton">-->
				<!--<input type="button" name="editgoods" class="editgoods" value="编辑" />-->
			<!--<input type="button" name="deletegoods" class="deletegoods" value="下架" />-->
			<!--</div>			-->
		<!--</div>-->
		<!-- -->
		<!--<div class="sellerGoodsList">-->
			<!--<img src="img/panda.jpg"/>-->
			<!-- -->
			<!--<ul  class="goodsInfo">&lt;!&ndash;这里只列出这写，商品的描述在商品详情页展示&ndash;&gt;-->
				<!--<li class="goodsName">名称:aaaaaaa</li>-->
				<!--<li class="goodsPlace">产地:aaaaaaa</li>-->
				<!--<li class="goodsPrice">价格:<span class="priceNum">40/g</span></li>-->
				<!--<li class="goodsNum">数量:<span class="number">400</span></li>-->
			<!--</ul>-->

			<!--<div class="goodsButton">-->
				<!--<input type="button" name="editgoods" class="editgoods" value="编辑" />-->
			<!--<input type="button" name="deletegoods" class="deletegoods" value="下架" />-->
			<!--</div>			-->
		<!--</div>-->
		<!-- -->
		<!-- -->
		<!--<div class="sellerGoodsList">-->
			<!--<img src="img/panda.jpg"/>-->
			<!-- -->
			<!--<ul  class="goodsInfo">&lt;!&ndash;这里只列出这写，商品的描述在商品详情页展示&ndash;&gt;-->
				<!--<li class="goodsName">名称:aaaaaaa</li>-->
				<!--<li class="goodsPlace">产地:aaaaaaa</li>-->
				<!--<li class="goodsPrice">价格:<span class="priceNum">1/袋</span></li>-->
				<!--<li class="goodsNum">数量:<span class="number">60</span></li>-->
			<!--</ul>-->

			<!--<div class="goodsButton">-->
				<!--<input type="button" name="editgoods" class="editgoods" value="编辑" />-->
			<!--<input type="button" name="deletegoods" class="deletegoods" value="下架" />-->
			<!--</div>			-->
		<!--</div>-->
		<!-- -->
		<!-- -->
		<!--<div class="sellerGoodsList">-->
			<!--<img src="img/panda.jpg"/>-->
			<!-- -->
			<!--<ul  class="goodsInfo">&lt;!&ndash;这里只列出这写，商品的描述在商品详情页展示&ndash;&gt;-->
				<!--<li class="goodsName">名称:aaaaaaa</li>-->
				<!--<li class="goodsPlace">产地:aaaaaaa</li>-->
				<!--<li class="goodsPrice">价格:<span class="priceNum">50/袋</span></li>-->
				<!--<li class="goodsNum">数量:<span class="number">5</span></li>-->
			<!--</ul>-->

			<!--<div class="goodsButton">-->
				<!--<input type="button" name="editgoods" class="editgoods" value="编辑" />-->
			<!--<input type="button" name="deletegoods" class="deletegoods" value="下架" />-->
			<!--</div>			-->
		<!--</div>-->
		<!-- -->
		<!--<div class="sellerGoodsList">-->
			<!--<img src="img/panda.jpg"/>-->
			<!-- -->
			<!--<ul  class="goodsInfo">&lt;!&ndash;这里只列出这写，商品的描述在商品详情页展示&ndash;&gt;-->
				<!--<li class="goodsName">名称:aaaaaaa</li>-->
				<!--<li class="goodsPlace">产地:aaaaaaa</li>-->
				<!--<li class="goodsPrice">价格:<span class="priceNum">20/kg</span></li>-->
				<!--<li class="goodsNum">数量:<span class="number">10</span></li>-->
			<!--</ul>-->

			<!--<div class="goodsButton">-->
				<!--<input type="button" name="editgoods" class="editgoods" value="编辑" />-->
			<!--<input type="button" name="deletegoods" class="deletegoods" value="下架" />-->
			<!--</div>			-->
		<!--</div>-->
		<!-- -->
		<!-- -->
		<!--<div class="sellerGoodsList">-->
			<!--<img src="img/panda.jpg"/>-->
			<!-- -->
			<!--<ul  class="goodsInfo">&lt;!&ndash;这里只列出这写，商品的描述在商品详情页展示&ndash;&gt;-->
				<!--<li class="goodsName">名称:aaaaaaa</li>-->
				<!--<li class="goodsPlace">产地:aaaaaaa</li>-->
				<!--<li class="goodsPrice">价格:<span class="priceNum">10/g</span></li>-->
				<!--<li class="goodsNum">数量:<span class="number">200</span></li>-->
			<!--</ul>-->

			<!--<div class="goodsButton">-->
				<!--<input type="button" name="editgoods" class="editgoods" value="编辑" />-->
			<!--<input type="button" name="deletegoods" class="deletegoods" value="下架" />-->
			<!--</div>			-->
		<!--</div>-->
		<!-- -->
		<!--<div class="sellerGoodsList">-->
			<!--<img src="img/panda.jpg"/>-->
			<!-- -->
			<!--<ul  class="goodsInfo">&lt;!&ndash;这里只列出这写，商品的描述在商品详情页展示&ndash;&gt;-->
				<!--<li class="goodsName">名称:aaaaaaa</li>-->
				<!--<li class="goodsPlace">产地:aaaaaaa</li>-->
				<!--<li class="goodsPrice">价格:<span class="priceNum">5/袋</span></li>-->
				<!--<li class="goodsNum">数量:<span class="number">30</span></li>-->
			<!--</ul>-->

			<!--<div class="goodsButton">-->
				<!--<input type="button" name="editgoods" class="editgoods" value="编辑" />-->
			<!--<input type="button" name="deletegoods" class="deletegoods" value="下架" />-->
			<!--</div>			-->
		<!--</div>-->
		<div id="mask"><!--黑色背景遮罩-->

		</div>
		<div id="alter"><!--修改数量与价格-->
			价格:<input type="number" name="alterPrice" id="alterPrice" value="" />
			<select name="danwei" id="danwei">
				<option value="1" selected="selected">/g</option>
				<option value="2">/kg</option>
				<option value="3">/袋</option>
			</select><br />
			数量:<input type="number" name="alterNum" id="alterNum" value="" />

				<div class="alterButton">
				<input type="button" name="save" id="save" value="保存" />
			<input type="button" name="cancel" id="cancel" value="取消" />
			</div>
		</div>
		<!-- -->
		
		<script type="text/javascript">


		</script>
		<!--点击下架按钮在前端删除此货物的结点-->
		<script src="js/jquery-2.1.1.js" type="text/javascript" charset="utf-8"></script>
			<script>

				function getUrl(){
					var str = location.href;//获取传过来的地址
					var arr = str.split("?");
					var str1 = arr[1];//获取？之后的内容
					var id = str1.split("=")[1];
					return id;
				}
				var Sql = document.getElementById('mySql');
//				先通过卖家id获取商品id
				$.ajax("/fengwei/goods/selectBySeller",{
					type:"get",
					data:{
						'sellerId':getUrl()
					},
					success:function(data){
						var dataObj = eval('('+data+')');
						console.log(dataObj)
						for(var i = 0;i<dataObj.length;i++){
							var Id = dataObj[i].goodsId;
							var Name = dataObj[i].goodsName;
							var City = dataObj[i].goodsCity;
							var Price = dataObj[i].goodsPrice;
							var Unit = dataObj[i].goodsUnit;
							var Size = dataObj[i].goodsSize;
//							调用该方法，将返回的商品信息传递下去
							picture(Id,Name,City,Price,Unit,Size)
						}
					},
					error:function(){

					}
				})

				function picture(Id,Name,City,Price,Unit,Size){
//				通过goodsId来查询图片信息
					$.ajax("/fengwei/file/selectByGoodsId",{
						type:"get",
						data:{
							'goodsId':Id
						},
						success:function(data1){
							var dataObj1 = eval('('+data1+')');
							console.log(dataObj1)
							var TheFile = dataObj1[0].fileId;
//							通过该方法将通过商品id获取到的图片与商品之间的联系表中的信息传递下去
							addAll(Id,TheFile,Name,City,Price,Unit,Size)
						},
						error:function(){
							alert(0)
						}
					})
				}
//				通过获取到的图片id，将图片信息获取到后，按照样式放到页面
				function addAll(Id,TheFile,Name,City,Price,Unit,Size){
					$.ajax("/fengwei/file/selectByFileId",{
						type:"get",
						data:{
							'fileId':TheFile
						},
						success:function(data2){
							var dataObj2 = eval('('+data2+')');
							console.log(Id+">>>"+Name+">>>"+City+">>>"+Price+">>>"+Unit+">>>"+Size+"++++"+data2)
//						按照格式来加数据布局页面
							var arr = dataObj2.fileUrl.split("\\");
							var src = arr[arr.length-2]+"/"+arr[arr.length-1];
							var div = document.createElement('div');//创建一个新的div
							div.setAttribute('class', 'sellerGoodsList');//给新的div设置class，class的样式在style里设置
							Sql.appendChild(div);
							div.innerHTML ="<img src='"+src+"'/>" +
								"<ul  class='goodsInfo'>" +
									"<li class='goodsName'>名称:"+Name+"</li>" +
									"<li class='goodsPlace'>产地:"+City+"</li>" +
									"<li class='goodsPrice'>价格:<span class='priceNum'>"+Price+""+Unit+"</span></li>" +
									"<li class='goodsNum'>数量:<span class='number'>"+Size+"</span></li>" +
								"</ul>" +
								"<div class='goodsButton'>" +
									"<input  _goodsid = '"+Id+"' type='button' name='editgoods' class='editgoods' value='编辑' />" +
									"<input _goodsid = '"+Id+"' type='button' name='deletegoods' class='deletegoods' value='下架' />" +
								"</div>"
//							调用编辑和删除的方法
							edit()
						},
						error:function(){
							alert(0)
						}
					})
				}

				function edit(){
					var sellerGoodsList=document.getElementsByClassName('sellerGoodsList');
					var editgoods=document.getElementsByClassName('editgoods');//获取编辑按钮
					var deletegoods=document.getElementsByClassName('deletegoods');//获取下架按钮
					var mask=document.getElementById('mask');//获取遮罩
					var alter=document.getElementById('alter');
					var saveButton=document.getElementById('save');
					var cancelButton=document.getElementById('cancel');

					for(var i=0;i<editgoods.length;i++){//点击编辑按钮即可编辑数量与价格
						mask.style.display='none';
						alter.style.display='none';
						editgoods[i].onclick=function(){
							mask.style.display='block';
							alter.style.display='block';
							var goodsId = $(this).attr("_goodsid");

							saveButton.onclick = function(){//保存
//								将数据上传更新，根据goodsid
								updata(goodsId,mask,alter);
							}
							cancelButton.onclick=function(){//取消
								mask.style.display='none';
								alter.style.display='none';
							}
						}
					}

					for(var i = 0;i<deletegoods.length;i++){
						deletegoods[i].onclick = function(){
							var goodsId = $(this).attr("_goodsid");
							dele(goodsId)
						}
					}
				}
				function updata(goodsId,newPrice,newdanwei,newNum){
					var newPrice = $("#alterPrice").val();
					var newdanwei = $("#danwei option:selected").text();
					var newNum = $("#alterNum").val();
					$.ajax("/fengwei/goods/change",{
						type:"post",
						data:{
							'goodsId':goodsId,
							'goodsPrice':newPrice,
							'goodsDanwei':newdanwei,
							'goodsNum':newNum
						},
						success:function(){
							mask.style.display='none';
							alter.style.display='none';
							window.location.reload();//刷新当前页面.
						},
						error:function(){
							alert(0)
						}
					})
				}
				//点击下架按钮在前端删除此货物的结点,先根据goodsid查询file_goods表中fileid的信息去删除file表中的图片，然后在删除goods表的信息，最后删除field——goods吧表中的信息它
				function dele(goodsId){
							$.ajax("/fengwei/fileGoods/delete",{
								type:"post",
								data:{
									'goodsId':goodsId
								},
								success:function(){
									alert(1)
//									成功之后要刷新页面
									window.location.reload();//刷新当前页面.

								},
								error:function(){
									alert(0)
								}
							})
				}
			</script>
	</body>
</html>
